import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import type { Task } from '../types/task';

export const useTaskStore = defineStore('task', () => {
  const tasks = ref<Task[]>([]);
  const totalTasks = computed(() => tasks.value.length);
  const completedTasks = computed(() => tasks.value.filter(task => task.completed).length);
  const uncompletedTasks = computed(() => tasks.value.filter(task => !task.completed).length);
  
  function addTask(title: string) {
    const newTask: Task = {
      id: Date.now(),
      title,
      completed: false
    };
    tasks.value.push(newTask);
  }
  
  function deleteTask(id: number) {
    tasks.value = tasks.value.filter(task => task.id !== id);
  }
  
  function toggleTaskStatus(id: number) {
    const task = tasks.value.find(task => task.id === id);
    if (task) {
      task.completed = !task.completed;
    }
  }
  
  return {
    tasks,
    totalTasks,
    completedTasks,
    uncompletedTasks,
    addTask,
    deleteTask,
    toggleTaskStatus
  };
});